package com.wangzifan.controller;

import com.wangzifan.entity.Grade;
import com.wangzifan.entity.PageBean;
import com.wangzifan.entity.Result;
import com.wangzifan.entity.Teacher;
import com.wangzifan.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/teachers")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 查询全部教师
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result queryTeacher(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询, 参数: {},{}",page,pageSize);
        PageBean pageBean = teacherService.page(page,pageSize);
        return Result.success(pageBean);
    }

    /**
     * 根据id查询教师
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryTeacherById(@PathVariable Integer id) {
        Teacher teacher = teacherService.queryTeacherById(id);
        return Result.success(teacher);
    }

    /**
     * 批量删除教师
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteTeacher(@PathVariable List<Integer> ids) {
        teacherService.deleteTeacher(ids);
        return Result.success();
    }

    /**
     * 添加教师
     * @param teacher
     * @return
     */
    @PostMapping()
    public Result addTeacher(@RequestBody Teacher teacher) {
        teacherService.addTeacher(teacher);
        return Result.success();
    }

    /**
     * 更新教师信息
     * @param teacher
     * @return
     */
    @PutMapping()
    public Result updateTeacher(@RequestBody Teacher teacher) {
        teacherService.updateTeacher(teacher);
        return Result.success();
    }
}
